package com.appkubes.mall.admin.application.activity.activity.service.impl;

import com.appkubes.mall.admin.application.activity.activity.dao.ActivityDao;
import com.appkubes.mall.admin.application.activity.activity.entity.dto.ActivityListDTO;
import com.appkubes.mall.admin.application.activity.activity.entity.vo.ActivityListVO;
import com.appkubes.mall.admin.application.activity.activity.entity.vo.ReleaseActivityVO;
import com.appkubes.mall.admin.application.activity.activity.service.ActivityService;
import com.appkubes.mall.admin.application.activity.goods.service.ActivityGoodsService;
import com.appkubes.mall.admin.application.activity.source.service.ActivitySourceService;
import com.appkubes.mall.core.entity.MallActivity;
import com.appkubes.mall.core.entity.MallActivityGoods;
import com.appkubes.mall.core.entity.MallActivitySource;
import com.appkubes.mall.core.service.impl.BaseMallActivityServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: Rongjie
 * @Date: 2020/3/4
 * @Description:
 */
@Service
public class ActivityServiceImpl extends BaseMallActivityServiceImpl implements ActivityService {

    @Autowired
    ActivitySourceService activitySourceService;

    @Autowired
    ActivityGoodsService activityGoodsService;

    @Autowired
    ActivityDao activityDao;

    @Transactional
    @Override
    public void releaseActivity(ReleaseActivityVO releaseActivityVO){
        MallActivity activity = new MallActivity();

        MallActivityGoods activityGoods = new MallActivityGoods();

        MallActivitySource activitySource = new MallActivitySource();

        BeanUtils.copyProperties(releaseActivityVO,activity);
        BeanUtils.copyProperties(releaseActivityVO,activityGoods);
        BeanUtils.copyProperties(releaseActivityVO,activitySource);

        this.saveOrUpdate(activity);
        activityGoods.setActivityKey(activity.getActivityKey());
        activityGoodsService.saveOrUpdate(activityGoods);
        activitySource.setActivityKey(activity.getActivityKey());
        activitySourceService.saveOrUpdate(activitySource);
    }

    @Override
    public List<ActivityListDTO> activityList(ActivityListVO activityListVO, Page pageInfo) {
        return activityDao.activityList(activityListVO.getSourceKey(),pageInfo);
    }
}
